
import { put, takeEvery } from 'redux-saga/effects'
import apiType, {
    GET_RECHARGE_WECHAT_ACCOUNT_LIST,
    GET_RECHARGE_ALIPAY_ACCOUNT_LIST,
    POST_WECHAT_PAY_APPLY,
    GET_WECHAT_PAY_APPLY,
    DELETE_WECHAT_PAY_ACCOUNT,
    POST_WECHAT_PAY_NAME,
    POST_ALIPAY_APPLY,
    GET_ALIPAY_APPLY,
    DELETE_ALIPAY_ACCOUNT,
    POST_ALIPAY_NAME,
    WEIXIN_IMAGE_UPLOAD,
    ALIPAY_IMAGE_UPLOAD,
    ALIPAY_AUTHORIZE,
    PAY_CONFIG_STORE,
    BIND_CONFIG_STORE,
    DELETE_CONFIG_STORE,
    GET_PAYMENT_GLOBAL_CONFIG,
    POST_PAYMENT_GLOBAL_CONFIG
} from "@constants/ActionTypes";
import { deleteAxios, getAxios, postAxios, putAxios } from "@utils/api";
// 查看微信收款账户配置列表
export const getWechatAccountList = (params, callback) => {
    return {
        type: apiType(GET_RECHARGE_WECHAT_ACCOUNT_LIST).request,
        params,
        callback
    }
}
// 查看支付宝收款账户配置列表
export const getAlipayAccountList = (params, callback) => {
    return {
        type: apiType(GET_RECHARGE_ALIPAY_ACCOUNT_LIST).request,
        params,
        callback
    }
}
// 支付宝当面付授权回调
export const alipayAuthorize = (params, callback) => {
    return {
        type: apiType(ALIPAY_AUTHORIZE).request,
        params,
        callback
    }
}
// 创建微信进件申请单并提交进件 / 基于已有申请重新进件
export const wechatPayApply = (params, callback) => {
    return {
        type: apiType(POST_WECHAT_PAY_APPLY).request,
        params,
        callback
    }
}
// 查看微信进件详细信息 基于收款账户ID
export const wechatPayApplyDetail = (params, callback) => {
    return {
        type: apiType(GET_WECHAT_PAY_APPLY).request,
        params,
        callback
    }
}
// 删除微信支付收款账号
export const deleteWechatAccount = (params, callback) => {
    return {
        type: apiType(DELETE_WECHAT_PAY_ACCOUNT).request,
        params,
        callback
    }
}
// 修改微信支付别名
export const modifyWechatAccountName = (params, callback) => {
    return {
        type: apiType(POST_WECHAT_PAY_NAME).request,
        params,
        callback
    }
}
// 创建支付宝进件申请单并提交进件 / 基于已有申请重新进件
export const alipayApply = (params, callback) => {
    return {
        type: apiType(POST_ALIPAY_APPLY).request,
        params,
        callback
    }
}
// 查看支付宝进件详情 基于收款账户ID
export const alipayApplyDetail = (params, callback) => {
    return {
        type: apiType(GET_ALIPAY_APPLY).request,
        params,
        callback
    }
}
// 删除支付宝支付收款账号
export const deleteAlipayAccount = (params, callback) => {
    return {
        type: apiType(DELETE_ALIPAY_ACCOUNT).request,
        params,
        callback
    }
}
// 修改支付宝支付别名
export const modifyAlipayAccountName = (params, callback) => {
    return {
        type: apiType(POST_ALIPAY_NAME).request,
        params,
        callback
    }
}
/**
 * 微信专用图片上传
 * @param image
 * @param imageType
 * @returns media_id
 */
export const wechatImageUpload = (params, callback) => {
    return {
        type: apiType(WEIXIN_IMAGE_UPLOAD).request,
        params,
        callback
    }
}
/**
 * 通用图片上传
 * @param image
 * @param imageType
 * @returns imageType imageUrl
 */
export const alipayImageUpload = (params, callback) => {
    return {
        type: apiType(ALIPAY_IMAGE_UPLOAD).request,
        params,
        callback
    }
}
/**
 * 获取配置列表
 */
export const getPayConfigList = (params, callback) => {
    return {
        type: apiType(PAY_CONFIG_STORE).request,
        params,
        callback
    }
}
/**
 * 获取全局配置
 */
export const getPaymentGlobalConfig = (params, callback) => {
    return {
        type: apiType(GET_PAYMENT_GLOBAL_CONFIG).request,
        params,
        callback
    }
}
/**
 * 修改全局配置
 */
export const postPaymentGlobalConfig = (params, callback) => {
    return {
        type: apiType(POST_PAYMENT_GLOBAL_CONFIG).request,
        params,
        callback
    }
}
/**
 * 绑定收款账号
 */
export const bindConfigStore = (params, callback) => {
    return {
        type: apiType(BIND_CONFIG_STORE).request,
        params,
        callback
    }
}
/**
 * 解绑收款账号
 */
export const deleteConfigStore = (params, callback) => {
    return {
        type: apiType(DELETE_CONFIG_STORE).request,
        params,
        callback
    }
}

const sagas = {
    *[apiType(GET_RECHARGE_WECHAT_ACCOUNT_LIST).request]({ params, callback }) {
        try {
            const response = yield getAxios('/pay/wechat/account', params)
            yield put({
                type: apiType(GET_RECHARGE_WECHAT_ACCOUNT_LIST).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_RECHARGE_WECHAT_ACCOUNT_LIST).failure });
        }
    },
    *[apiType(GET_RECHARGE_ALIPAY_ACCOUNT_LIST).request]({ params, callback }) {
        try {
            const response = yield getAxios('/pay/alipay/account', params)
            yield put({
                type: apiType(GET_RECHARGE_ALIPAY_ACCOUNT_LIST).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_RECHARGE_ALIPAY_ACCOUNT_LIST).failure });
        }
    },
    *[apiType(POST_WECHAT_PAY_APPLY).request]({ params, callback }) {
        try {
            const response = yield postAxios('/pay/wechat/apply', params)
            yield put({
                type: apiType(POST_WECHAT_PAY_APPLY).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(POST_WECHAT_PAY_APPLY).failure });
        }
    },
    *[apiType(GET_WECHAT_PAY_APPLY).request]({ params, callback }) {
        try {
            const response = yield getAxios('/pay/wechat/apply', params)
            yield put({
                type: apiType(GET_WECHAT_PAY_APPLY).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_WECHAT_PAY_APPLY).failure });
        }
    },
    *[apiType(DELETE_WECHAT_PAY_ACCOUNT).request]({ params, callback }) {
        try {
            const response = yield deleteAxios('/pay/wechat/account', params)
            yield put({
                type: apiType(DELETE_WECHAT_PAY_ACCOUNT).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(DELETE_WECHAT_PAY_ACCOUNT).failure });
        }
    },
    *[apiType(POST_WECHAT_PAY_NAME).request]({ params, callback }) {
        try {
            const response = yield putAxios('/pay/wechat/account', params)
            yield put({
                type: apiType(POST_WECHAT_PAY_NAME).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(POST_WECHAT_PAY_NAME).failure });
        }
    },
    *[apiType(POST_ALIPAY_APPLY).request]({ params, callback }) {
        try {
            const response = yield postAxios('/pay/alipay/apply', params)
            yield put({
                type: apiType(POST_ALIPAY_APPLY).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(POST_ALIPAY_APPLY).failure });
        }
    },
    *[apiType(GET_ALIPAY_APPLY).request]({ params, callback }) {
        try {
            const response = yield getAxios('/pay/alipay/apply', params)
            yield put({
                type: apiType(GET_ALIPAY_APPLY).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_ALIPAY_APPLY).failure });
        }
    },
    *[apiType(DELETE_ALIPAY_ACCOUNT).request]({ params, callback }) {
        try {
            const response = yield deleteAxios('/pay/alipay/account', params)
            yield put({
                type: apiType(DELETE_ALIPAY_ACCOUNT).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(DELETE_ALIPAY_ACCOUNT).failure });
        }
    },
    *[apiType(POST_ALIPAY_NAME).request]({ params, callback }) {
        try {
            const response = yield putAxios('/pay/alipay/account', params)
            yield put({
                type: apiType(POST_ALIPAY_NAME).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(POST_ALIPAY_NAME).failure });
        }
    },
    *[apiType(WEIXIN_IMAGE_UPLOAD).request]({ params, callback }) {
        try {
            const response = yield postAxios('/pay/config/image', params)
            yield put({
                type: apiType(WEIXIN_IMAGE_UPLOAD).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(WEIXIN_IMAGE_UPLOAD).failure });
        }
    },
    *[apiType(ALIPAY_IMAGE_UPLOAD).request]({ params, callback }) {
        try {
            const response = yield postAxios('/pay/wechat/image', params)
            yield put({
                type: apiType(ALIPAY_IMAGE_UPLOAD).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(ALIPAY_IMAGE_UPLOAD).failure });
        }
    },
    *[apiType(ALIPAY_AUTHORIZE).request]({ params, callback }) {
        try {
            const response = yield getAxios('/pay/alipay/authorize', params)
            yield put({
                type: apiType(ALIPAY_AUTHORIZE).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(ALIPAY_AUTHORIZE).failure });
        }
    },
    *[apiType(PAY_CONFIG_STORE).request]({ params, callback }) {
        try {
            const response = yield getAxios('/pay/config/store', params)
            yield put({
                type: apiType(PAY_CONFIG_STORE).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(PAY_CONFIG_STORE).failure });
        }
    },
    *[apiType(BIND_CONFIG_STORE).request]({ params, callback }) {
        try {
            const response = yield postAxios('/pay/config/store', params)
            yield put({
                type: apiType(BIND_CONFIG_STORE).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(BIND_CONFIG_STORE).failure });
        }
    },
    *[apiType(DELETE_CONFIG_STORE).request]({ params, callback }) {
        try {
            const response = yield deleteAxios('/pay/config/store', params)
            yield put({
                type: apiType(DELETE_CONFIG_STORE).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(DELETE_CONFIG_STORE).failure });
        }
    },
    *[apiType(GET_PAYMENT_GLOBAL_CONFIG).request]({ params, callback }) {
        try {
            const response = yield getAxios('/pay/config/global', params)
            yield put({
                type: apiType(GET_PAYMENT_GLOBAL_CONFIG).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_PAYMENT_GLOBAL_CONFIG).failure });
        }
    },
    *[apiType(POST_PAYMENT_GLOBAL_CONFIG).request]({ params, callback }) {
        try {
            const response = yield postAxios('/pay/config/global', params)
            yield put({
                type: apiType(POST_PAYMENT_GLOBAL_CONFIG).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(POST_PAYMENT_GLOBAL_CONFIG).failure });
        }
    },
}


export default Object.keys(sagas).map(item => {
    return function* s() {
        yield takeEvery(item, function* (args) {
            try {
                yield sagas[item](args)
            } catch (e) {
                console.log(e)
            }
        })
    }
})